//! @Author: DengLibin
//! @Date: Create in 2024-07-25 16:06:28
//! @Description:
//!
//!
mod test {

 

    use std::{fs, sync::Arc};

    use log::info;
    use rust_my_tools::{app::init_log, component::file_content_extactor::file_text_extractor::{spawn_extract_text, FileText}, file_scanner::file_extractor::extract_file};
    use tokio::{runtime::Builder, sync::mpsc};
    fn cre_runtime() -> tokio::runtime::Runtime {
        // 创建一个 Tokio 运行时，配置线程池的大小
        Builder::new_multi_thread()
            .worker_threads(1) // 设置工作线程的数量
            .max_blocking_threads(16) // 设置阻塞任务的最大线程数
            .enable_all()
            .build()
            .unwrap()
    }
    //测试文件文本提取
    #[test]
    pub fn test_file_extractor() {
        let r = cre_runtime();
        let s = my_util_lib::date::get_sys_timestamp_millis();
        let r = r.block_on(async {
            init_log().await?;
            info!("日志初始化完成");
            let (tx, mut rx) = mpsc::channel::<String>(1000); // 创建通道，设置缓冲区大小
            let (f_tx, mut f_rx) = mpsc::channel::<FileText>(1000); // 创建通道，设置缓冲区大小
            let a_f_tx = Arc::new(f_tx);

            //接收文件
            let f1 = async move {
                //捕获a_f_tx
                //接收提取的文件
                while let Some(file_path) = rx.recv().await {
                    info!("收到文件:{}", file_path);
                    spawn_extract_text(file_path.clone(), a_f_tx.clone()).await;
                    // spawn_extract_text(file_path.clone(), a_f_tx.clone()).await;
                    // spawn_extract_text(file_path.clone(), a_f_tx.clone()).await;
                   
                }
                //循环结束,
                println!("收到文件完成")
                //f1任务结束，销毁a_f_tx
            };

            let f2 = async move{
                 //接收文件内容
                 while let Some(file_text) = f_rx.recv().await {
                    info!("收到文件内容:{}-->{}",file_text.file_path,  file_text.text.chars().count());
                }
                println!("收到文件内容完成")
            };
       

            //开始提取文件
            let f3 = extract_file(r#"D:\yiscn\调研\四川志书pdf\四川志书pdf\PDF-四川年鉴2011-2020\四川年鉴2020.pdf"#, tx);
            let (_r1,_r2,  r3) = tokio::join!(f1, f2, f3);
            r3
        });
        let e = my_util_lib::date::get_sys_timestamp_millis();
        println!("耗时:{} ms", e - s);
        r.unwrap();
    }




    #[test]
    pub fn count_file() {
        let count = do_count(r#"D:\yiscn\测试文件"#);
        println!("文件（夹）数量:{}", count);
    }

    fn do_count(file_path: &str) -> usize {
        let metadata = fs::metadata(file_path).unwrap();
        if metadata.is_file() {
            return 1;
        }

        let entrys = fs::read_dir(file_path).unwrap();
        let mut count: usize = 1_usize;
        for entry in entrys {
            let entry = entry.unwrap();
            let path = entry.path();

            let o = path.as_os_str().to_str().unwrap();
            count += do_count(o);
        }
        return count;
    }
}
